package com.yuelan.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuelan.apartment.domain.ApaRentBill;
import com.yuelan.app.domain.vo.AppRentBillVo;
import com.yuelan.app.mapper.UserRentBillMapper;
import com.yuelan.app.service.UserRentBillService;
import com.yuelan.common.constant.HttpStatus;
import com.yuelan.common.core.page.TableDataInfo;
import com.yuelan.common.exception.GlobalException;
import com.yuelan.common.utils.SecurityUtils;
import com.yuelan.common.utils.bean.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static com.yuelan.app.constant.BillType.*;

/**
 * @description:
 * @Author: ZhaoYi
 * @date: 24-6-30 3:55
 */

@Service
public class UserRentBillServiceImpl implements UserRentBillService {

    @Autowired
    private UserRentBillMapper userRentBillMapper;

    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);


    @Override
    public TableDataInfo billList(int pageNum, int pageSize) {
        List<AppRentBillVo> appRentBillVoList = userRentBillMapper
                .selectBill(SecurityUtils.getUserId());

        List<AppRentBillVo> billList = appRentBillVoList
                .stream().map(bill ->{
                    AppRentBillVo appRentBillVo = new AppRentBillVo();
                    BeanUtils.copyBeanProp(appRentBillVo , bill);
                    appRentBillVo.setOverdueDay(0);
                    appRentBillVo.setOverdueAmount(BigDecimal.ZERO);
                    BigDecimal total = bill.getRent();
                    if (bill.getOverdueAmount() != null) {
                        appRentBillVo.setOverdueDay(bill.getOverdueDay());
                        appRentBillVo.setOverdueAmount(bill.getOverdueAmount());
                        total = total.add(bill.getOverdueAmount());
                    }
                    appRentBillVo.setStatus(getBillStatus(bill.getStatus()));
                    appRentBillVo.setTotalAmount(total);
                    return appRentBillVo;
                }).collect(Collectors.toList());

        List<AppRentBillVo> pageDate = billList
                .stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize)
                .collect(Collectors.toList());
        TableDataInfo res = new TableDataInfo();
        res.setCode(HttpStatus.SUCCESS);
        res.setRows(pageDate);
        res.setTotal(billList.size());
        return res;
    }

    private String getBillStatus(String type) {
        if (Uncollected.getStatus().equals(type))
            return Uncollected.getRes();
        if (Collected.getStatus().equals(type))
            return Collected.getRes();
        if (Overdue.getStatus().equals(type))
            return Overdue.getRes();
        return type;
    }


    @Override
    public AppRentBillVo view(Long id) {
        AppRentBillVo appRentBillVo = new AppRentBillVo();
        AppRentBillVo billVo = userRentBillMapper.selectBillById(id);
        BeanUtils.copyBeanProp(appRentBillVo , billVo);
        appRentBillVo.setOverdueDay(0);
        appRentBillVo.setOverdueAmount(BigDecimal.ZERO);
        BigDecimal total = billVo.getRent();
        if (billVo.getOverdueAmount() != null) {
            appRentBillVo.setOverdueDay(billVo.getOverdueDay());
            appRentBillVo.setOverdueAmount(billVo.getOverdueAmount());
            total = total.add(billVo.getOverdueAmount());
        }
        appRentBillVo.setStatus(getBillStatus(billVo.getStatus()));
        appRentBillVo.setTotalAmount(total);
        return appRentBillVo;
    }


    @Override
    public boolean expireBill() {
        LambdaQueryWrapper<ApaRentBill> rentBillLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rentBillLambdaQueryWrapper
                .eq(ApaRentBill::getTenantId, SecurityUtils.getUserId())
                .eq(ApaRentBill::getStatus, Overdue.getStatus());
        return userRentBillMapper.exists(rentBillLambdaQueryWrapper);
    }


}
